import React, { Component } from "react";
import MainLayout from "../../../component/mainLayout/mainLayout";

import Button from "../../../component/button/index";
/* import AgreeCheckbox from "../../../../component/agreeCheckbox/index"; */
import { InputItem, Toast } from "antd-mobile";
import "./index.scss";
//import verCode from "../../../component/verCode/index";

import XHQBConfig from "../../../config/XHQBConfig";
/* import BankItem from "./bankItem.js"; */
/* import BackModal from '../../../component/backModal/index'; */
import yhTools from "../../../tools/yh";

const SimplePwd = [
	"111111",
	"222222",
	"333333",
	"444444",
	"555555",
	"666666",
	"777777",
	"888888",
	"999999",
	"000000",
	"123456",
	"234567",
	"345678",
	"456789",
	"987654",
	"876543",
	"765432",
	"654321",
	"543210",
	"012345",
	"098765",
	"567890"
];
/* @verCode()
@connect(
  state => ({
    supportedBankList: state.bankReducer.supportedBankList || [],
    applyNo: state.getAppPlatformsDatas.applyNo,
    lnid: state.getAppPlatformsDatas.lnid,
    ocrInfo: state.livenessReducer
  }),
  dispatch => ({
    addNewBank: value => dispatch(addNewBank(value)),
    setSupportedBankList: value => dispatch(setSupportedBankList(value)),
    getSelectedBankList: value => dispatch(getSelectedBankList(value)),
    getSupportedBankList: value => dispatch(getSupportedBankList(value)),
    checkUserBankInfo: () => dispatch(checkUserBankInfo())
  })
) */
class paymentpassword extends Component {
	constructor(props) {
		super(props);
		this.state = {
			isVisible: false,
			accountNo: "",
			/* verifyCode: "", */
			channelCode: "YHB",
			userBankPhone: "",
			isRead: true,
			messageSerialNumber: "",
			verifier: "",
			from: "",
			isBackModalVisible: false,
			newpsd: "",
			newpsdagain: "",
			memberId: "",
			//psdchecked: false,
			oldpsd: "",

			hasSendCode: false
		};
	}
	componentDidMount = () => {
		const _that = this;
		//const from = yhTools.getUrlParam("from");
		/* this.setState({
            from
        }); */
		/* const { ocrInfo,checkUserBankInfo,supportedBankList,getSupportedBankList } = this.props;
        if(from === 'bankList' ) {
            !ocrInfo.userName && checkUserBankInfo();  
        } else {
            getSupportedBankList()
        } */

		/* let objs = {
      url: XHQBConfig.queryMemberInfo,
      
    };
    yhTools.request(objs).then(res => {
      console.log(res);

      if (res.code == 0) {
        _that.setState({
          userBankPhone: res.data.member.mobile,
          memberId: res.data.member.memberId
        });
      }
    }); */
		let phonenumber = JSON.parse(window.sessionStorage.getItem("phone"));

		_that.setState({
			userBankPhone: phonenumber
		});
	};
	/* toggleVisible = isVisible => {
        this.setState({
            isVisible
        })
    }; */
	replaceStar(str) {
		if (typeof str !== "string") {
			return "";
		}
		let length = str.length;
		if (length < 7) {
			return str;
		}
		let star = "*".repeat(length - 7);
		return str.replace(/^(\d{3})\d*(\d{4})|(x)$/, `$1${star}$2`);
	}

	replaceallStar(str) {
		if (typeof str !== "string") {
			return "";
		}
		let length = str.length;
		let newstr = "";
		for (let i = 0; i < length; i++) {
			newstr += "*";
		}
		return newstr;
	}
	/* 验证简单密码 */
	checkSimplePwd(e) {
		if (SimplePwd.indexOf(e) > -1) {
			return true;
		} else {
			return false;
		}
	}
	/* 发送验证码 */
	/* sendMsgCode = () => {
    const{newpsdagain,newpsd}=this.state
    const { sendCode, isClick } = this.props.verCode;
    if(newpsdagain.length!==6||newpsd.length!==6){
      Toast.info("请完成密码填写", 1);
      return false;
    }else if(newpsdagain!==newpsd){
      Toast.info("两次密码输入不一致", 1);
      return false;
    }else if(this.checkSimplePwd(newpsd)){
      Toast.info("密码过于简单", 1);
      return false;
    }
    else if (!isClick) return;

    Toast.loading("加载中...", 100);
    let objs = {
      url: XHQBConfig.sendSmsCodeRetrievePassword,
      params:{}
    };
    yhTools.request(objs).then(res => {
      if (res.code == 0) {
        Toast.hide();
        sendCode();
        this.state({
          hasSendCode: true
        });
        Toast.info("验证码发送成功", 1);
      }
    }); */
	/* const { channelCode, accountNo, userBankPhone, from } = this.state;
    const phoneReg = {
      valueParams: userBankPhone.replace(/\s/g, "")
    };
    if (!checkTool.isPhoneNo(phoneReg)) {
      Toast.info("请输入正确的手机号码", 2);
      return false;
    }
    const params = {
      channelCode,
      accountNo: submitCardRegexp(accountNo)
    };
    let objs = {
      url:
        from === "liveness"
          ? InterfaceG.openAccountSendCode
          : InterfaceG.sendCode,
      params:
        from === "liveness"
          ? { ...params, bankMobilePhone: userBankPhone }
          : { ...params, userBankPhone }
    };
    //Toast.loading("加载中...", 100);

    yhTools.request(objs).then(res => {
      Toast.hide();
      if (res.code === "0") {
        
        const { messageSerialNumber, verifier } = res.data;
        console.log(messageSerialNumber, verifier);
        this.setState({
          messageSerialNumber,
          verifier
        });
      } else {
        Toast.info(res.message, 2);
      }
    }); 
  };*/
	/* openAccount = () => {
        const { applyNo, lnid } = this.props;
        const {
            accountNo,
            verifyCode,
            userBankPhone,
            messageSerialNumber
        } = this.state;
        if (this.checkCanSubmit(accountNo, userBankPhone, verifyCode)) {
            let objs = {
                url: InterfaceG.openAccount,
                params: {
                    applyNo,
                    lnid,
                    cardNo: submitCardRegexp(accountNo),
                    verifyCode,
                    bankMobilePhone: userBankPhone,
                    messageSerialNumber,
                    channelCode: "YHB"
                }
            };
            Toast.loading("加载中...", 100);
            console.log(JSON.stringify(objs));
            yhTools.request(objs).then(res => {
                Toast.hide();
                if (res.code === "0") {
                    const cardNo = res.data.electronicCardNo;
                    this.props.history.push(
                        `/openAccountResult?status=success&cardNo=${cardNo}`
                    );
                } else {
                    this.props.history.push("/openAccountResult?status=fail");
                }
            });
        }
    }; */
	InputOnChange = (key, value) => {
		this.setState({
			[key]: value.trim()
		});
	};
	/* InputonBlur = val => {
    var reg = /^\d+$|^\d+[.]?\d+$/;
    let res = reg.test(val);
    if (!res) {
      Toast.info("请输入数字");
    } else if (val.length !== 6) {
      Toast.info("请输入6位密码");
      res=false
    }
    this.setState({
      psdchecked: res
    });
  }; */
	/* toggleAgree = () => {
        this.setState(prevState => {
            return { isRead: !prevState.isRead };
        });
    }; */

	/* checkCanSubmit = (verifyCode) => {
     const bankReg = {
      valueParams: cardNo.replace(/\s/g, ""),
      validateRules: "/^[0-9]{12,19}$/"
    };
    const phoneReg = {
      valueParams: phoneNum.replace(/\s/g, "")
    };
    if (!checkTool.isBankNo(bankReg)) {
      Toast.info("请输入正确的银行卡号", 2);
      return false;
    }
    if (!checkTool.isPhoneNo(phoneReg)) {
      Toast.info("请输入正确的手机号码", 2);
      return false;
    } 
    
    if (!verifyCode.length) {
      Toast.info("请填写验证码");
      return false;
    }
    if (verifyCode.length !== 6) {
      Toast.info("验证码输入错误");
      return false;
    }
    return true;
  };*/
	/* addNewBankFn = () => {
        const {
            channelCode,
            accountNo,
            userBankPhone,
            verifyCode,
            messageSerialNumber,
            verifier,
            from
        } = this.state;
        if (this.checkCanSubmit(accountNo, userBankPhone, verifyCode)) {
            let objs = {
                url: InterfaceG.addNewCard,
                params: {
                    channelCode,
                    accountNo: submitCardRegexp(accountNo),
                    userBankPhone,
                    verifyCode,
                    messageSerialNumber,
                    verifier
                }
            };
            Toast.loading("加载中...", 100);
            yhTools.request(objs).then(res => {
                Toast.hide();
                if (res.code === "0") {
                    this.props.getSelectedBankList({ channelCode });
                    setTimeout(() => {
                        this.props.history.push(`/${addBankcardPageMap[from]}`);
                    }, 0);
                } else {
                    Toast.info(res.message, 2);
                }
            });
        }
    }; */
	/* 跳转到忘记密码 */
	gotoforget() {
		this.props.history.push({
			pathname: "/forgetpassword",
			state: this.state.userBankPhone
		});
	}
	saveBank = () => {
		/* const { from } = this.state;
    if (from === "liveness") {
      this.openAccount();
    } else {
      this.addNewBankFn();
    } */

		const {
			//verifyCode,

			newpsdagain,
			newpsd,
			oldpsd
		} = this.state;
		//const { sendCode, isClick } = this.props.verCode;

		if (newpsdagain.length !== 6 || newpsd.length !== 6) {
			Toast.info("请完成密码填写", 1);
			return false;
		} else if (newpsdagain !== newpsd) {
			Toast.info("两次密码输入不一致", 1);
			return false;
		} else if (oldpsd == newpsd || oldpsd == newpsdagain) {
			Toast.info("新密码与原密码一致", 1);
			return false;
		} else if (this.checkSimplePwd(newpsd)) {
			/* else if(!verifyCode.length){
      Toast.info("请填写验证码");
      return false;
    }else if(verifyCode.length !== 6){
      Toast.info("验证码输入错误");
      return false;
    } */
			Toast.info("密码过于简单", 1);
			return false;
		}
		Toast.loading("加载中...", 100);
		let objs = {
			url: XHQBConfig.setPayPassword,
			params: {
				//smsCode:verifyCode,
				confirmPassword: newpsdagain,
				password: newpsd,
				oriPassword: oldpsd
			},
			_alertShow: false
		};
		yhTools.request(objs).then(res => {
			if (res.code == 0) {
				Toast.hide();
				Toast.info("密码修改成功", 2);
				setTimeout(() => {
					this.props.history.push("/xhqbIndex");
				}, 2000);
			} else {
				Toast.hide();
				Toast.info(res.message, 2);
			}
		});
		/* if(this.checkCanSubmit(verifyCode)){
      let objs = {
        url: "/member/setPayPassword",
        params:{
          memberId,
          password:newpsd
        }
      };
      yhTools.request(objs).then(res => {
        console.log(res);
        
      });
    } */
	};
	//查看协议
	/* openContract = (e, type) => {
        e.stopPropagation();
        const { userName, idNo } = this.props.ocrInfo;
        const commonParams = {
            personName: userName,
            idCardCode: idNo,
            time1: `${year}年${month}月${day}日`,
            time2: `${year}年${month}月${day}日`
        };
        const paramsHuaTong = {
            ...commonParams,
            signName: userName
        };
        this.props.history.push({
            pathname: agreementPageMap[type],
            params: type === "huaTongAccount" ? paramsHuaTong : commonParams
        });
    }; */

	render() {
		const { newpsd, newpsdagain, oldpsd } = this.state;
		const _pageParams = {
			header: {
				title: "修改支付密码",
				rightButt: "忘记密码",
				rightButtCallBack: this.gotoforget.bind(this)
			},

			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		//const { countDown, isClick } = this.props.verCode;
		return (
			<div>
				<MainLayout pageParams={_pageParams} /* handleBack={this.showModal} */>
					{/* <BackModal
                        product={lnid}
                        isVisible={isBackModalVisible}
                        onClick={this.onModalClick}
                    /> */}
					<div className="add-bank-container">
						{/* <InputItem
                            className="my-input"
                            defaultValue={ocrInfo.userName}
                            value={ocrInfo.userName}
                            disabled={true}
                        >
                            持卡人
                        </InputItem>
                        <InputItem
                            className="my-input my-clear1"
                            placeholder="请输入银行卡号"
                            type="bankCard"
                            maxLength={30}
                            clear
                            defaultValue={accountNo}
                            onChange={this.InputOnChange.bind(
                                this,
                                "accountNo"
                            )}
                        >
                            银行卡号
                            <img
                                alt="guide"
                                className="icon-help"
                                src={BankIcon}
                                onClick={() => this.toggleVisible(true)}
                            />
                        </InputItem> */}
						{/* <InputItem
              maxLength="11"
              clear
              type="tel"
              disabled
              defaultValue={userBankPhone}
              value={this.replaceStar(userBankPhone)}
              className="my-input"
              onChange={this.InputOnChange.bind(this, "userBankPhone")}
            >
              手机号
            </InputItem> */}
						<InputItem
							maxLength="6"
							clear
							type="number"
							placeholder="请填写旧密码"
							defaultValue={oldpsd}
							//value={this.replaceallStar(newpsd)}
							className="my-input security"
							onChange={this.InputOnChange.bind(this, "oldpsd")}
							//onBlur={this.InputonBlur.bind(this)}
						>
							旧密码
						</InputItem>
						<InputItem
							maxLength="6"
							clear
							type="number"
							placeholder="请填写新密码"
							defaultValue={newpsd}
							//value={this.replaceallStar(newpsd)}
							className="my-input security"
							onChange={this.InputOnChange.bind(this, "newpsd")}
							//onBlur={this.InputonBlur.bind(this)}
						>
							新密码
						</InputItem>
						<InputItem
							maxLength="6"
							clear
							type="number"
							placeholder="请填写重新输入密码"
							defaultValue={newpsdagain}
							className="my-input security"
							//value={this.replaceallStar(newpsdagain)}
							onChange={this.InputOnChange.bind(this, "newpsdagain")}
							//onBlur={this.InputonBlur.bind(this)}
						>
							确认密码
						</InputItem>

						{/* <InputItem
              clear
              placeholder="输入验证码"
              maxLength={6}
              minLength={6}
              type="number"
              className="my-input my-clear2"
              onChange={this.InputOnChange.bind(this, "verifyCode")}
            >
              验证码
              {
                <span className="get-code" onClick={this.sendMsgCode}>
                  {isClick
                    ? hasSendCode
                      ? "重新发送"
                      : "获取验证码"
                    : countDown}
                </span>
              }
            </InputItem> */}

						<Button
							style={{ marginTop: "60px" }}
							onClick={this.saveBank.bind(this)}
						>
							确认
						</Button>
					</div>
				</MainLayout>
			</div>
		);
	}
}

export default paymentpassword;
